package kosta.controller;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import kosta.model.board.file.FileBoardService;
import kosta.model.board.file.FileBoardVO;
import kosta.model.member.MemberService;
import kosta.model.member.MemberVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FileBoardController {
	@Resource(name="fileBoardServiceImpl")
	private FileBoardService fileBoardService;
	@Resource(name="memberServiceImpl")
	private MemberService memberService;
	/*@Resource(name="fileReplyServiceImpl")
	private FileReplyService fileReplyService;*/
	private String path="C:\\java-kosta\\final-workspace\\javabox-ver2-paging-pl\\WebContent\\upload\\";
	
	/**
	 * 자료실 목록 페이지
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_fileList.do")
	public ModelAndView getListfileBoard() throws Exception {
		List<FileBoardVO> list = fileBoardService.getListBoard();
		//System.out.println(list+ " con");
		return new ModelAndView("file_list.fileboard","list",list);
	}
	
	/**
	 * 자료실 상세보기 페이지
	 * @param fileNo
	 * @param fileCount
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_fileView.do")
	public ModelAndView fileView(FileBoardVO vo) throws Exception {
		//System.out.println(vo+" con");
		fileBoardService.updateCount(vo);
		FileBoardVO fvo = fileBoardService.findByNo(vo.getFileNo());
		//System.out.println(fvo+ " con");
		return new ModelAndView("file_view.fileboard","fvo",fvo);
	}
	
	/**
	 * 자료실 다운로드
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_fileDownload.do")
	public ModelAndView fileDownload(String filename) throws Exception{
		//System.out.println(filename+" 다운로드 실행");
		HashMap map = new HashMap();
		map.put("path", path);
		//System.out.println(map);
		return new ModelAndView("downloadView",map);
	}
	
	/**
	 * 자료실 글쓰기 페이지로 이동
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_goInsertFileBoard.do")
	public ModelAndView goInsertFileBoard() throws Exception{
		return new ModelAndView("file_write.fileboard");
	}
	
	/**
	 * 자료실 업로드 & 글쓰기
	 * @param vo
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/board_insertFileUpload.do")
	public ModelAndView insertFileUpload(FileBoardVO vo, HttpSession session) throws Exception {
		//System.out.println("first "+vo);
		MultipartFile mFile = vo.getFile();
			if(mFile.getSize()>0) {
				File copyFile = new File(path+mFile.getOriginalFilename());
				try {
					mFile.transferTo(copyFile);
					//System.out.println("fileUpload ok! "+copyFile.getPath());
				} catch(IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		//System.out.println(vo.getFile().getOriginalFilename());
		vo.setFileName(vo.getFile().getOriginalFilename());
		fileBoardService.insertBoard(vo);
		//System.out.println("second "+vo);
		FileBoardVO fvo = fileBoardService.findByNo(vo.getFileNo());
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");	
		fileBoardService.pointPlus(vo.getFileWriter());
		session.setAttribute("mvo", memberService.login(mvo));
		return new ModelAndView("file_view.fileboard","fvo",fvo);
	}
	
	/**
	 * 자료실 수정페이지로 이동
	 * @param fileNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_goUpdateFileBoard.do")
	public ModelAndView goUpdateFileBoard(int fileNo) throws Exception {
		FileBoardVO fvo = fileBoardService.findByNo(fileNo);
		return new ModelAndView("file_update.fileboard","fvo",fvo);
	}
	
	/**
	 * 자료실 수정 페이지 업로드된 파일 삭제
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_deleteUploadFile.do")
	public ModelAndView deleteUploadFile(FileBoardVO vo) throws Exception{
		File f = new File(path+vo.getFileName());
		f.delete();
		vo.setFileName("");
		fileBoardService.erasureUploadFile(vo);
		return new ModelAndView("redirect:board_goUpdateFileBoard.do?fileNo="+vo.getFileNo());
	}
	
	/**
	 * 자료실 글수정
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_updateFileUpload.do")
	public ModelAndView updateFileBoard(FileBoardVO vo) throws Exception {
		//System.out.println(vo+" updateFileBoard");
		if(vo.getFile()!=null) {
			vo.setFileName(vo.getFile().getOriginalFilename());
		}
		fileBoardService.updateBoard(vo);
		FileBoardVO fvo = fileBoardService.findByNo(vo.getFileNo());
		return new ModelAndView("file_view.fileboard","fvo",fvo);
	}
	
	/**
	 * 자료실 글삭제
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_erasureFileBoard.do")
	public ModelAndView erasureFileBoard(FileBoardVO vo, HttpSession session) throws Exception {
		//System.out.println("file delete"+vo);
		if(vo.getFileName()!=null) {
			File f = new File(path+vo.getFileName());
			f.delete();
		}
		fileBoardService.erasureFileBoard(vo.getFileNo());
		List<FileBoardVO> list = fileBoardService.getListBoard();
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");	
		session.setAttribute("mvo", memberService.login(mvo));
		return new ModelAndView("file_list.fileboard","list",list);
	}
}
